package com.blrs.service.area.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.blrs.domain.SysArea;
import com.blrs.mapper.SysAreaMapper;
import org.springframework.stereotype.Service;
import com.blrs.service.area.ISysAreaService;

import javax.annotation.Resource;

/**
 * 区域信息Service业务层处理
 * 
 * @author yuxiang.chen
 * @date 2023-08-08
 */
@Service
public class SysAreaServiceImpl implements ISysAreaService 
{
    @Resource
    private SysAreaMapper sysAreaMapper;

    /**
     * 查询区域信息
     * 
     * @param AreaId 区域信息主键
     * @return 区域信息
     */
    @Override
    public SysArea selectSysAreaByAreaId(String AreaId)
    {
        return sysAreaMapper.selectSysAreaByAreaId(AreaId);
    }

    /**
     * 查询区域信息列表
     * 
     * @param sysArea 区域信息
     * @return 区域信息
     */
    @Override
    public List<SysArea> selectSysAreaList(SysArea sysArea)
    {
        List<SysArea> list=sysAreaMapper.selectSysAreaList(sysArea);
        List<SysArea> areas=new ArrayList<>();
        Iterator<SysArea> it = list.iterator();
        while (it.hasNext()){
            SysArea area=it.next();
            if(area.getParentId().equals("000000")){
                areas.add(area);
                it.remove();
            }
        }
        for (SysArea area : areas) {
            this.areaTree(list,area);
        }
        return areas;
    }

    @Override
    public List<SysArea> selectSysArea(SysArea sysArea)
    {
        return sysAreaMapper.selectSysAreaList(sysArea);
    }

    /**
     * 新增区域信息
     * 
     * @param sysArea 区域信息
     * @return 结果
     */
    @Override
    public int insertSysArea(SysArea sysArea)
    {
        return sysAreaMapper.insertSysArea(sysArea);
    }

    /**
     * 修改区域信息
     * 
     * @param sysArea 区域信息
     * @return 结果
     */
    @Override
    public int updateSysArea(SysArea sysArea)
    {
        return sysAreaMapper.updateSysArea(sysArea);
    }

    /**
     * 批量删除区域信息
     * 
     * @param AreaIds 需要删除的区域信息主键
     * @return 结果
     */
    @Override
    public int deleteSysAreaByAreaIds(String[] AreaIds)
    {
        return sysAreaMapper.deleteSysAreaByAreaIds(AreaIds);
    }

    /**
     * 删除区域信息信息
     * 
     * @param AreaId 区域信息主键
     * @return 结果
     */
    @Override
    public int deleteSysAreaByAreaId(String AreaId)
    {
        return sysAreaMapper.deleteSysAreaByAreaId(AreaId);
    }
    private void areaTree(List<SysArea> areas,SysArea obj){
        Iterator<SysArea> it=areas.iterator();
        while(it.hasNext()){
            SysArea area=it.next();
            if(area.getParentId().equals(obj.getAreaId())){
                obj.setList(area);
                boolean f=false;
                for (SysArea sysArea : areas) {
                    if(sysArea.getParentId().equals(area.getAreaId())){
                        f=true;
                    }
                }
                if(f){
                    areaTree(areas,area);
                }
            }
        }
    }
}

